home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 2595 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  3.6 KB

  1. Path: locutus.rchland.ibm.com!usenet
  2. From: pstaite@vnet.ibm.com
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Q:order of evaluation
  5. Date: 18 Jan 1996 15:27:24 GMT
  6. Organization: IBM OS/2 Device Driver Development  Rochester, MN
  7. Message-ID: <4dloss$lje@locutus.rchland.ibm.com>
  8. References: <4dfhlu$a33$1@mhafn.production.compuserve.com> <4di5qb$qda$1@mhadg.production.compuserve.com>
  9. Reply-To: pstaite@vnet.ibm.com
  10. NNTP-Posting-Host: warpone.rchland.ibm.com
  11. X-Newsreader: IBM NewsReader/2 v1.2
  12.  
  13. In <4di5qb$qda$1@mhadg.production.compuserve.com>, Holger Maier <100336.3326@CompuServe.COM> writes:
  14. >Please note that in
  15. >  int i=1;int j=i+(i+=1);
  16. >the value of i is modified only once. The ARM states that an 
  17. >expression where a value is modified more than once is undefined 
  18. >(ie: i = i++; ). But since i is not modified multiply and assignment
  19. >returns the assigned value, this expression should be reasonable.
  20. >Any comments??
  21.  
  22. No, no, no, no, NO!  Sheesh, won't these kind of constructs ever die?
  23.  
  24. From the ARM, chapter 5, "Expressions" pg. 46
  25.  
  26.   The order of evaluation of subexpressions is determined by the
  27.   expression grammar.  The usual mathematical rules for associativity 
  28.   and commutativity of operators may be applied only where the operators
  29.   really are associative and commutative.  Except where noted, the order
  30.   of evaluation of operands of individual operators is undefined.
  31.  
  32. The key phrase here is "...order of evaluation of operands ... is 
  33. undefined."
  34.  
  35. That means that in the expression:
  36.  
  37. j = i + ( i += 1 );
  38.  
  39. The operands of operator + are i and a subexpression.  The compiler is 
  40. free to "evaluate" either operand first, then the other.  The compiler 
  41. can do it either way.  It could do it one way on one compile, and the
  42. other on another compile just because some optimization or debug flags 
  43. changed.
  44.  
  45. The subexpression ( i += 1 ) has side effects on i.  However, _exactly_
  46. when those side effects "take effect" on i is not defined.  It only has 
  47. to happen before the next sequence point.  The only sequence point in 
  48. this statement is the end of evaluation of the full expression. (ref 1-8
  49. of the DWP)
  50.  
  51. The side effect could take place immediately on evaluation.  If so, then
  52. i would be changed as soon as the ( i += 1 ) operand for operator + was
  53. evaluated.  So, the LHS i could then evaluate to either 1 or 2, 
  54. depending on the (undefined) order of evaluation of the operands to 
  55. operator +.
  56.  
  57. Therefore, between sequence points this statement has side effects on i,
  58. and two subexpressions referencing i.  This is not good.  In chapter 5, 
  59. para 4 of the DWP it states:
  60.  
  61.   Between the previous and next sequence point a scalar object shall 
  62.   have its stored value modified at most once by the evaluation of an
  63.   expression.  Furthermore, the prior value shall be accessed only to
  64.   determine the value to be stored.  The requirements of this paragraph
  65.   shall be met for each allowable ordering of the subexpressions of a
  66.   full expression; otherwise the behavior is undefined.
  67.  
  68. This statement violates the "furthermore" part of the paragraph.  The 
  69. scalar i is accessed twice, once to get the value to be stored (from the
  70. += operator) and again to evaluate the LHS operand for operator +.  This
  71. makes the result of executing this statement undefined.
  72.  
  73. Some day I'll write a compiler that upon seeing this generates code to:
  74.  
  75.   1) Send an email note of appology to Bjarne Stroustrup for abusing
  76.      his language.
  77.  
  78.   2) Reformat all disks connected to the machine.
  79.  
  80. Well, maybe not #1, he'd be inundated with email ;-)
  81.  
  82. Remember, short compact code is neat and fun etc. but don't pack too 
  83. much into one statement.
  84.  
  85.  
  86. Phil Staite, team OS/2
  87. internet: pstaite@vnet.ibm.com  internal: pstaite@rchland
  88.  
  89.